package com.gzwanchuang.lszg.module.account;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gzwanchuang.lszg.R;
import com.gzwanchuang.lszg.base.BaseActivity;
import com.gzwanchuang.lszg.base.MainApplication;
import com.gzwanchuang.lszg.help.UserHelper;
import com.gzwanchuang.lszg.net.Apis;
import com.gzwanchuang.lszg.net.NetworkManager;
import com.gzwanchuang.lszg.net.WebApis;
import com.gzwanchuang.lszg.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.lszg.proto.Connect;
import com.gzwanchuang.lszg.proto.Recharge;
import com.gzwanchuang.lszg.utils.StatusBarUtil;
import com.gzwanchuang.lszg.view.VerificationCodeEditText;
import com.gzwanchuang.lszg.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TiXianCodeActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.input_code)
    VerificationCodeEditText inputCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian_code);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        ButterKnife.bind(this);
        tvTitle.setText("");
        ivBack.setOnClickListener(v -> finish());
        tvPhone.setText("短信验证码发送至：" + UserHelper.getUser().getMember_mobile());
        getCode();
        tvCode.setOnClickListener(v -> getCode());
        inputCode.setOnInputListener(new VerificationCodeEditText.OnInputListener() {
            @Override
            public void OnEdittextChange(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void OnInputOk(String codeNum) {
                showProgress();
                Recharge.Pd_cash_add.Builder builder = Recharge.Pd_cash_add.newBuilder();
                builder.setCaptcha(codeNum).setPassword(getIntent().getStringExtra("password")).setCardId(getIntent().getStringExtra("card_id"))
                        .setPdcAmount(getIntent().getStringExtra("money")).setWalletType(getIntent().getType());
                NetworkManager.INSTANCE.post(Apis.post_tixian, builder.build().toByteArray(), new OnRequestCallBack() {
                    @Override
                    public void onOk(byte[] response) throws InvalidProtocolBufferException {
                        hideProgress();
                        MainApplication.isRef = true;
                        MainApplication.finishTopActivity(2);
                        WebViewActivity.startActivity(mContext, WebApis.cashWithdrawal_apply + "?member_id=" + Recharge.Pd_cash_add.parseFrom(response).getId());
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        hideProgress();
                        showToast(errorMessage);
                        inputCode.clean();
                    }
                });

            }
        });
        showSoftInputFromWindow(inputCode.editCodeNum);
    }

    private void getCode() {
        Connect.captcha.Builder builder = Connect.captcha.newBuilder();
        builder.setPhone(UserHelper.getUser().getMember_mobile());
        builder.setType("10");
        Connect.captcha data = builder.build();
        NetworkManager.INSTANCE.post(Apis.getMobileCode, data.toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {

            }

            @Override
            public void onError(int code, String errorMessage) {

                if (code == 200) {
                    countDownReSend(tvCode, 60);
                } else {
                    showToast(errorMessage);
                }
            }
        });
    }

    public void showSoftInputFromWindow(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }


    /*------------------------ 验证码倒计时方法------------------------*/
    public void countDownReSend(final TextView textView, long sec) {
        if (textView == null) return;
        textView.setTag(textView.getTextColors());
        textView.setEnabled(false);
        new CountDownTimer(sec * 1000, 1000) {
            public void onTick(long millisUntilFinished) {
                if (textView == null) {
                    cancel();
                    return;
                }
                textView.setText((millisUntilFinished / 1000) + "秒后重新发送");
            }

            public void onFinish() {
                if (textView == null)
                    return;
                textView.setText("点击重新获取");
                textView.setEnabled(true);
                textView.setSelected(false);
            }
        }.start();
    }
}
